version 14.1
set more off
  quietly log
  local logon = r(status)
  if "`logon'" == "on" {
	log close 
	}
log using SPID_v1.2_static.log, text replace


/*	***************************************************************		*/
/* 	Author:			Scott LaCombe and Frederick J. Boehmke				*/
/*	Date:			June 21, 2021										*/
/*  File:			SPID_v1.2_static.do	  			      				*/
/*	Purpose:		Generate dynamic rate scores from SPID v1.2 data.	*/
/*	Requires:		stpolinn.ado										*/
/*  Input File: 	SPID_v1.2.dta, 										*/
/*  				SPID_v1.2_policies.dta 								*/
/*  Output File: 	SPID_v1.2_dynamic.dta								*/
/*	***************************************************************		*/

	/*********************************************************************/
	/* Uncomment and run these commands if you need to install stpolinn. */
	/*********************************************************************/


* net from http://myweb.uiowa.edu/fboehmke/stata/stpolinn
* net install stpolinn
	

	/**************************************************/
	/* Create the dynamic innovation scores data set. */
	/**************************************************/

		/* Read in adoption data. */

use SPID_v1.2.dta, clear
merge m:1 policy using SPID_v1.2_policies.dta, nogenerate assert(match) keep(match)

		/* Replace Alaska and Hawaii with -9 before they become states since some */
		/* policies include them and others don't (even if not right-censored). */

replace adopt_year = -9 if inlist(state, "Alaska", "Hawaii") & first_year <= 1958

		/* Restrict to policies diffusing in 1912 or after */
	
drop if first_year < 1912
		
		/* Calculate dynamic score. */

stpolinn adopt_year, longform(policy) score(rate) dynamic interval(2) id(state) ///
	generate(inn_rate_bi) excludevalue(-9) saving(SPID_v1.2_dynamic.dta, replace)

		/* Calculate smoothed score. This uses the default bandwidth of 1.5. */

stpolinn adopt_year, longform(policy) score(rate) smoothed id(state) ///
	generate(inn_rate_sm) excludevalue(-9) replace 

		/* Combine the two scores into one data set. */
	
	merge 1:1 statenam time using SPID_v1.2_dynamic.dta, keep(match master using) nogenerat
	
		/* Clean up the dataset. */
	
	drop period inn_rate_sm_year
	rename time year

	label variable statenam 		"State Name"
	label variable year 			"Year"
	label variable inn_rate_bi		"Dynamic Innovation Rate - biennial"
	label variable inn_rate_sm		"Dynamic Innovation Rate - smoothed"
	label variable inn_rate_sm_se	"Dynamic Innovation Rate - smoothed (standard error)"

	order statenam year inn_rate_bi inn_rate_sm inn_rate_sm_se
	
	compress

	label data "Dynamic rate scores from SPID v1.2"

	saveold SPID_v1.2_dynamic.dta, replace version(13)


clear
log close
exit, STATA

